library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✔ tibble 3.1.7 ✔ dplyr 1.0.9
## ✔ tidyr 1.2.0 ✔ stringr 1.4.0
## ✔ readr 2.1.2 ✔ forcats 0.5.1
## ✔ purrr 0.3.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks plotly::filter(), stats::filter()
## ✖ dplyr::lag() masks stats::lag()
df <- as.data.frame(ggplot2::midwest)
RColorBrewer::display.brewer.all()

plot_ly(
data = df,
x = ~perchsd,
y = ~percollege,
type = "scatter",
mode = "markers",
color = ~state,
colors = "Set1"
)
plot_ly(
data = df,
x = ~perchsd,
y = ~percollege,
type = "scatter",
mode = "markers",
color = ~state,
colors = c("red", "blue", "black", "#32a852", "#c10dd1")
)
df %>%
group_by(state) %>%
summarize(tot = sum(poptotal)) %>%
plot_ly(
x = ~state,
y = ~tot,
type = "bar",
color = ~tot,
colors = "YlOrRd"
)
## Warning: textfont.color doesn't (yet) support data arrays
## Warning: textfont.color doesn't (yet) support data arrays
df %>%
group_by(state) %>%
summarize(tot = sum(poptotal)) %>%
plot_ly(
x = ~state,
y = ~tot,
type = "bar",
color = ~tot,
colors = c("#1B98E0","black")
)
## Warning: textfont.color doesn't (yet) support data arrays
## Warning: textfont.color doesn't (yet) support data arrays
df %>%
plot_ly(
x = ~state,
y = ~percbelowpoverty,
type = "scatter",
color = ~percbelowpoverty,
colors = "PuOr"
) %>%
layout(plot_bgcolor = "#bababa")# increases contrast between the dots and background
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
df %>%
plot_ly(
x = ~state,
y = ~percbelowpoverty,
type = "scatter",
color = ~percbelowpoverty,
colors = c("#4e9665","white","#c26d11")
) %>%
layout(plot_bgcolor = "#bababa")# increases contrast between the dots and background
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode